//
//  IMImageCellCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/7/6.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "IMImageCellCell.h"
#import <YBImageBrowser/YBImageBrowser.h>

//#define kImageMaxWidth kScreenW/3.0
//#define kImageMaxHeight kImageMaxWidth*kScreenH/kScreenW
#define kImageMaxHeight kWidth(120)

@interface IMImageCellCell ()<YBImageBrowserDataSource>

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIView *messageContentView;
@property (nonatomic, strong) UIImageView *messageImgView;
@property (nonatomic, strong) UIActivityIndicatorView *indicator;
@property (nonatomic, strong) UIButton *reSendBtn;

@end

@implementation IMImageCellCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setMessage:(IMMessageEntity *)message
{
    _message = message;
    if (message.image) {
//        CGFloat w = MIN(message.image.size.width, kImageMaxWidth);
//        CGFloat h = MIN(message.image.size.height, kImageMaxHeight);
//        [self.messageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.width.equalTo(@(w));
//            make.height.equalTo(@(h));
//        }];
        self.messageImgView.image = message.image;
//        [self layoutIfNeeded];
    }
    if (message.filePath) {
        [self.messageImgView sd_setImageWithURL:[NSURL URLWithString:message.filePath] placeholderImage:PlaceHolderImage];
//        [self.messageImgView sd_setImageWithURL:[NSURL URLWithString:message.filePath] placeholderImage:PlaceHolderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//            CGFloat w = MIN(image.size.width, kImageMaxWidth);
//            CGFloat h = MIN(image.size.height, kImageMaxHeight);
//            [self.messageContentView mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.width.equalTo(@(w));
//                make.height.equalTo(@(h));
//            }];
//            [self.tableView beginUpdates];
//            [self.tableView endUpdates];
//
//        }];
    }
    
    if (message.isAnimation) {
        [self startAnimation];
    }
    else {
        [self stopAnimation];
    }
    if (message.isShowReSend) {
        [self showReSend];
    }
    else {
        [self hiddenReSend];
    }
}

- (void)startAnimation
{
    self.indicator.hidden = NO;
    [self.indicator startAnimating];
}

- (void)stopAnimation
{
    self.indicator.hidden = YES;
    [self.indicator stopAnimating];
}

- (void)showReSend
{
    self.reSendBtn.hidden = NO;
}

- (void)hiddenReSend
{
    self.reSendBtn.hidden = YES;
}


- (void)setupSubViews
{
    [self addSubview:self.icon];
    [self addSubview:self.nameLab];
    [self addSubview:self.messageContentView];
    [self.messageContentView addSubview:self.messageImgView];
    [self addSubview:self.indicator];
    [self addSubview:self.reSendBtn];
    
    
    self.messageContentView.backgroundColor = kColorGradientBegin;
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-kWidth(15));
        make.top.equalTo(self).offset(kWidth(10));
        make.width.height.equalTo(@(kWidth(40)));
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.right.equalTo(self.icon.mas_left).offset(-kWidth(6));
    }];
    [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.nameLab);
        make.top.equalTo(self.nameLab.mas_bottom).offset(kWidth(6));
        make.left.greaterThanOrEqualTo(self).offset(kWidth(kScreenW/4.0));
        make.height.lessThanOrEqualTo(@(kImageMaxHeight));
//        make.width.lessThanOrEqualTo(@(kImageMaxWidth));
        make.bottom.equalTo(self);
    }];
    [self.messageImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.messageContentView).offset(kWidth(0));
        make.bottom.right.equalTo(self.messageContentView).offset(-kWidth(0));
    }];
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.messageContentView);
        make.right.equalTo(self.messageContentView.mas_left).offset(-kWidth(6));
    }];
    [self.reSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.messageContentView);
        make.right.equalTo(self.messageContentView.mas_left).offset(-kWidth(6));
    }];
    [self.reSendBtn addTarget:self action:@selector(reSendAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(browserImage:)];
    [self.messageImgView addGestureRecognizer:tap];
}

- (void)reSendAction:(id)sender
{
    self.message.isShowReSend = NO;
    self.message.isAnimation = YES;
    [self hiddenReSend];
    [self startAnimation];
    if (self.reSendHandle) {
        self.reSendHandle(self.message);
    }
}

- (void)browserImage:(UITapGestureRecognizer *)tap
{
    [self browserImage];
}

// 图片预览
- (void)browserImage
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = 0;
    [browser show];
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return 1;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    UIImage *img = self.messageImgView.image;
    data.imageBlock = ^__kindof UIImage * _Nullable{
        return img;
    };
    data.sourceObject = self.messageImgView;
    return data;
}

- (UIImageView *)icon
{
    if (!_icon) {
        _icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"person_headpic"]];
        _icon.layer.cornerRadius = kWidth(20);
        _icon.layer.masksToBounds = YES;
        _icon.backgroundColor = [UIColor whiteColor];
    }
    return _icon;
}

- (UILabel *)nameLab
{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = kColor666666;
        _nameLab.font = [UIFont systemFontOfSize:12];
    }
    return _nameLab;
}

- (UIView *)messageContentView
{
    if (!_messageContentView) {
        _messageContentView = [[UIView alloc] init];
    }
    return _messageContentView;
}

- (UIImageView *)messageImgView
{
    if (!_messageImgView) {
        _messageImgView = [[UIImageView alloc] init];
        _messageImgView.contentMode = UIViewContentModeScaleAspectFill;
        _messageImgView.layer.masksToBounds = YES;
        _messageImgView.userInteractionEnabled = YES;
    }
    return _messageImgView;
}

- (UIActivityIndicatorView *)indicator
{
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicator.hidden = YES;
    }
    return _indicator;
}

- (UIButton *)reSendBtn
{
    if (!_reSendBtn) {
        _reSendBtn = [[UIButton alloc] init];
        [_reSendBtn setImage:[UIImage imageNamed:@"reSend"] forState:UIControlStateNormal];
        _reSendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_reSendBtn setTitleColor:kColor666666 forState:UIControlStateNormal];
        _reSendBtn.hidden = YES;
    }
    return _reSendBtn;
}


@end
